import { Suggestion, SuggestionResult } from '@/types/data'
import { SearchAction } from '@/types/store'

type SearchState = {
  suggestion: Suggestion['options']
  suggestionResult: SuggestionResult
}
const InitialState: SearchState = {
  suggestion: [],
  suggestionResult: {
    page: 1,
    per_page: 10,
    results: [],
    total_count: 0,
  },
}
export const search = (
  state = InitialState,
  action: SearchAction
): SearchState => {
  if (action.type === 'search/suggestion') {
    return {
      ...state,
      suggestion: action.payload,
    }
  }
  if (action.type === 'search/clearSuggestion') {
    return {
      ...state,
      suggestion: [],
    }
  }
  if (action.type === 'search/getSuggestionResult') {
    return {
      ...state,
      suggestionResult: action.payload,
    }
  }
  return state
}
